package com.unismc.wixindemo.service.impl;

import com.unismc.wixindemo.config.WeChatMpConfig;
import com.unismc.wixindemo.handler.ScanHandler;
import com.unismc.wixindemo.handler.SubscribeHandler;
import com.unismc.wixindemo.handler.UnsubscribeHandler;
import com.unismc.wixindemo.service.EventService;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class EventServiceImpl implements EventService {
	
	@Autowired
	private SubscribeHandler subscribeHandler;
	
	@Autowired
	private UnsubscribeHandler unsubscribeHandler;
	
	@Autowired
	private ScanHandler scanHandler;

	@Autowired
	private WeChatMpConfig weChatMpConfig;
	
	@Override
	public void getEvent(WxMpXmlMessage wxMessage) {
		WxSessionManager sessionManager=null;
		Map<String, Object> context = new HashMap<String, Object>();
		System.out.println("Event:"+wxMessage.getEvent());
		try {
			switch(wxMessage.getEvent()) {
			case "subscribe":
					subscribeHandler.handle(wxMessage, context, weChatMpConfig.wxMpService(), sessionManager);
				break;
			case "unsubscribe":
				unsubscribeHandler.handle(wxMessage, context, weChatMpConfig.wxMpService(), sessionManager);
				break;
			}
		} catch (Exception e) {
		}
	}
	
}
